'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceSenddepositapplyCtrl
 * @description
 * # FinanceSenddepositapplyCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceSenddepositapplyCtrl', function ($scope, $rootScope, openApi, Util, $location, $routeParams, localStorageService, FormSubmit, TaskReviewService) {
        var id = $scope.process_id = $routeParams.id
        // 底部款项类型选框添加
        $scope.payWayList = [{
            key: "银行保函",
            value: 1
        }, {
            key: "柜台转账",
            value: 2
        }, {
            key: "网银转账",
            value: 3
        }, {
            key: "银行汇票",
            value: 4
        }];
        $scope.choosePeople = function () {
            var relModuleObj = {
                name: "FinanceFBzjCtrl",
                selectTab: "deposit_select_project",
                selectTabContent: "工程名称"
            };
            // 将列表的数据序列化存入缓存以便回来时候使用
            $scope.param.fundSoruceDetail = JSON.stringify($scope.param.fundSoruceList)
            $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList)
            Util.savaParamForReview($scope.param, id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.disabled = function () {
            var checked = true;
            if (!$scope.param) {
                return
            }
            if ($scope.param.payType == 3) {
                checked = false;
            }
            return checked
        };
        $scope.addfundSorucePenson = function () {
            $rootScope.$emit('showmasker');
            $scope.showFundPerson = true;
        };
        $scope.deletedItem = function (index) {
            $scope.param.fundSoruceList.splice(index, 1);
            $scope.index = -1;
        };
        $scope.confirmFundPerson = function () {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.fundSoruceList.push($scope.picker);
            // 隐藏表单
            hideFundPerson();
        };
        var hideFundPerson = function () {
            $scope.picker = {};
            $rootScope.$emit('closemasker');
            $scope.showFundPerson = false;
        };
        $scope.$on('closemasker', function () {
            hideFundPerson()
        });
        $scope.cacelFundPerson = function () {
            hideFundPerson();
        };
        // 验证是否输入完整
        var checkPicker = function (data) {
            var check = true;
            if (!data.money) {
                Util.Toast("请输入付款金额");
                check = false;
                return check;
            }
            return check;

        };
        $scope.submit = function (operationId) {

            $scope.param.fundSoruceDetail = angular.toJson($scope.param.fundSoruceList)
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.depositApply;
            options.param = angular.copy($scope.param, {});
            options.param["formBidApply.formId"] = options.param.formBidApply && options.param.formBidApply.formId || "";
            delete options.param.fundSoruceList
            delete options.param.fundSoruceDetail
            delete options.param.receiveList
            delete options.param.formBidApply
            FormSubmit.submit(options,$scope);
        };
        $scope.validate = function () {
            if (!$scope.param.projectName) {
                Util.Toast("请选择工程名称");
                return;
            }
            if (!$scope.param.payWay) {
                Util.Toast("请选择转款方式");
                return;
            }
            if (!$scope.param.endDate) {
                Util.Toast("请选择到账截至日期");
                return;
            }
            if (!$scope.param.fundSoruceMoney) {
                Util.Toast("请填写来款金额");
                return;
            }
            if (!$scope.param.remark) {
                Util.Toast("请填写备注");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        $scope.swipe = function (index) {
            $scope.index = index;
        }
        $scope.resetlist = function (index) {
            $scope.index = -1;
        }

        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                $scope.param.payType = 1;
                // 根据后台返回的字符串创建列表，后台没有返回的话就初始化一个列表
                $scope.param.fundSoruceList = $scope.param.fundSoruceDetail && JSON.parse($scope.param.fundSoruceDetail) || [];
            });
        };
        // 初始化
        init();
    });